package com.ruoyi.controller;

import com.ruoyi.common.R;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.Broker;
import com.ruoyi.common.core.domain.model.Store;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.OSSFileUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mapper.StoreMapper;
import com.ruoyi.service.IBrokerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 经纪人Controller
 * 
 * @author ruoyi
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/broker/broker")
public class BrokerController extends BaseController
{
    @Autowired
    private IBrokerService brokerService;
    @Autowired
    StoreMapper storeMapper;
    /**
     * 查询经纪人列表
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:list')")
    @GetMapping("/list")
    public TableDataInfo list(Broker broker)
    {
        startPage();
        List<Broker> list = brokerService.selectBrokerList(broker);
        return getDataTable(list);
    }

    @PostMapping("/storeslistfor")
    public List<Store> storeslistfor(){

        List<Store> stores = storeMapper.selectStoreList(null);
        System.out.println("guoyihang");
        return stores;
    }
    /**
     * 导出经纪人列表
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:export')")
    @Log(title = "经纪人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Broker broker)
    {
        List<Broker> list = brokerService.selectBrokerList(broker);
        ExcelUtil<Broker> util = new ExcelUtil<Broker>(Broker.class);
        util.exportExcel(response, list, "经纪人数据");
    }

    /**
     * 获取经纪人详细信息
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(brokerService.selectBrokerById(id));
    }

    /**
     * 新增经纪人
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:add')")
    @Log(title = "经纪人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Broker broker)
    {
        return toAjax(brokerService.insertBroker(broker));
    }

    /**
    * @description
    * @author Gaosir
    * @return * @return: null
    * oss上传
    * @date 2024/9/14 11:36
    */
    @PostMapping("/BrokderOss")
    @Anonymous
    public R BrokderOss(MultipartFile file){
        OSSFileUtil ossFileUtil = new OSSFileUtil();
        String s=null;
        try {
             s = ossFileUtil.uploadFile(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.OK(s);
    }


    /**
     * 修改经纪人
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:edit')")
    @Log(title = "经纪人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Broker broker)
    {
        return toAjax(brokerService.updateBroker(broker));
    }

    /**
     * 删除经纪人
     */
    @PreAuthorize("@ss.hasPermi('broker:broker:remove')")
    @Log(title = "经纪人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(brokerService.deleteBrokerByIds(ids));
    }
    /**
     * @description
     * @author Gaosir
     * @return * @return: null
     * 查询门店下有哪些 经纪人
     * @date 2024/9/17 20:05
     */
    @PostMapping("/InquireBroker")
    public R InquireBroker(@RequestBody Broker broker){

        return brokerService.InquireBroker(broker);
    }
}
